package com.zhuiyun.project.api.tag.service.impl;

import com.zhuiyun.project.api.tag.entity.TagRequest;
import com.zhuiyun.project.api.tag.entity.UserTag;
import com.zhuiyun.project.api.tag.mapper.TagMapper;
import com.zhuiyun.project.api.tag.entity.Tag;
import com.zhuiyun.project.api.tag.model.TagModel;
import com.zhuiyun.project.api.tag.service.TagService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TagServiceImpl
 * @Description TODO
 * @Author XMY
 * @Date 2023/12/13 16:42
 **/
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<Tag> getTagList() {
        return tagMapper.selectAll();
    }
    /**
     * @Author XMY
     * @Description 新增用户个性标签
     * @Date 2023/12/14 9:31
     * @Return
     * @param request
     */
    @Transactional
    @Override
    public CommonResult savaTag(TagRequest request) throws CommonException {
        if (ValidatorUtil.ValidationObject(request)) {
            int userId = request.getId();
            int insertResult = 0;
            List<Integer> tagIds = request.getTagId();
            // 先删除中间表数据
            tagMapper.deleteUserTag(userId);
            // 批量插入新的中间表数据
            List<UserTag> userTags = new ArrayList<>();
            for (Integer tagId : tagIds) {
                UserTag userTag = new UserTag();
                userTag.setUserId(userId);
                userTag.setTagId(tagId);
                insertResult = tagMapper.batchInsertUserTag(userTag);
                userTags.add(userTag);
            }
            if (insertResult > 0) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            } else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /**
     * @Author XMY
     * @Description 新增用户个性标签
     * @Date 2023/12/14 9:35
     * @Return
     * @param tagModel
     */
    @Override
    public CommonResult insertTag(TagModel tagModel) throws CommonException{
        if(ValidatorUtil.ValidationObject(tagModel)){
            Tag tag = new Tag();
            BeanUtils.copy(tagModel, tag);
            int i = tagMapper.insert(tag);
            if (i > 0) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            } else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }

    }
}
